<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        echo $this->getAt();
    }

    public function getAt()
    {

    	if(S('access_token')){

    		return S('access_token');

    	}else{

    		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".C('APPID')."&secret=".C('APPSECRET');

	    	$data = file_get_contents($url);

	    	$data = json_decode($data,true);

	    	if(isset($data['access_token'])){

	    		S('access_token',$data['access_token'],7200);

	    		return $data['access_token'];
	    	}

    	}
    	
    }

    public function getwxIp()
    {
    	$at = $this->getAt();
    	$url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$at;

    	$data = file_get_contents($url);

	    $data = json_decode($data,true);

	    var_dump($data);
    }
    public function setmenu()
    {
    	$at = $this->getAt();
    	$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$at;


    	$arr = array(

    		'button'=>array(
    			array(
    				'type'=>'view',
    				'name'=>'社区',
    				'url'=>'http://1o533n8683.imwork.net/P201704/bbs/index.php/home/login/wxLogin'
    				),
    			array(

    				'name'=>'我是一级',
    				'sub_button'=>array(
    					array(
	    				'type'=>'view',
	    				'name'=>'腾讯',
	    				'url'=>'http://www.qq.com'
	    				),
    					array(
	    				'type'=>'view',
	    				'name'=>'新浪',
	    				'url'=>'http://www.sina.com'
	    				),
    					array(
	    				'type'=>'view',
	    				'name'=>'网易',
	    				'url'=>'http://www.163.com'
	    				)
    				)
    			),
    			array(
    				'type'=>'view',
    				'name'=>'百度一下',
    				'url'=>'http://www.baidu.com'
    				)
    			)
    		);
    	$data = json_encode($arr,JSON_UNESCAPED_UNICODE);

    	$res = $this->wcurl($url,$data);

    	var_dump($res);

    }

    public function wcurl($url,$data)
    {

    	// var_dump($url,$data);exit;
    	$ch = curl_init();

    	curl_setopt($ch, CURLOPT_URL, $url);

    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    	curl_setopt($ch, CURLOPT_POST, 1);

    	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	    'Content-Type: application/json',
	    'Content-Length: ' . strlen($data)));
		
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

	    $out = curl_exec($ch);


	    $data =  json_decode($out,true);

	    return $data;
    }

    public function getUserList()
    {
    	$at = $this->getAt();
    	$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$at;

    	$data = file_get_contents($url);

    	$data = json_decode($data,true);

	    return $data['data']['openid'];

    }
    public function qunfa()
    {
    	$openids = $this->getUserList();

    	$at = $this->getAt();

    	$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$at;



    	$data = array(
    		'touser'=>$openids,
    		'msgtype'=>'text',
    		'text'=>array(
    			'content'=>'大家好，测试群发'
    			)
    		);

    	$data = json_encode($data,JSON_UNESCAPED_UNICODE);

    	$res = $this->wcurl($url,$data);

    	var_dump($res);

    }
    public function moban()
    {
    	$openid = "oF5Z2v5yzYfsCYP2oEDfz4ns94p0";

    	$at = $this->getAt();

    	$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$at;



    	$data = array(
	    		'touser'=>$openid,
	    		'template_id'=>'CQQkgL16s7n7PezCDvR5ktaLjQ9KWG_digc_KhYA8xI',
	    		'url'=>'http://www.baidu.com',
	    		'data'=>array(
	    			'addr'=>array(
	    					'value'=>'丽景华苑东边厕所',
	    					"color"=>"#173177"
	    			),
	    			'time'=>array(
	    					'value'=>date('Y-m-d H:i:s'),
	    					"color"=>"#173177"
	    			),
	    			'money'=>array(
	    					'value'=>'2000',
	    					"color"=>"#173177"
	    			)
	    		)
    		);

    	$data = json_encode($data,JSON_UNESCAPED_UNICODE);

    	$res = $this->wcurl($url,$data);

    	var_dump($res);
    }
}